package com.manager.system.controller;

import com.manager.common.utils.ObjectUtils;
import com.manager.common.vo.PageVO;
import com.manager.common.vo.ResultVO;
import com.manager.model.System_Permission;
import com.manager.system.service.PermissionService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class PermissionController {

    @Autowired
    private PermissionService permissionServiceImpl;

    /**
     * 获取所有权限数据
     * @param model
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getAllPermission")
    public PageVO getAllPermission(System_Permission model, HttpServletRequest request, HttpServletResponse response) throws IOException{
        PageVO pageVO = permissionServiceImpl.getAllPermission(model);
        return pageVO;
    }

    @RequestMapping("/delete")
    public ResultVO delete(System_Permission model) {
        try{
            permissionServiceImpl.deletePermission(model);
            return ResultVO.success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    /**
     * 获取权限tree数据
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/getMenuJsonData")
    public ResultVO getMenuJsonData(String type, String table_id, HttpServletRequest request, HttpServletResponse response) throws IOException{
        try {
            JSONArray json = permissionServiceImpl.getPermissionJsonData(type,table_id);
            return ResultVO.success(json);
        }catch (Exception e){
            e.printStackTrace();
        }
      return ResultVO.failed();
    }

    @RequestMapping("/getAllPermissionTree")
    public ResultVO getAllPermissionTree(){
        try{
            List<System_Permission> list = permissionServiceImpl.getAllPermission();
            return ResultVO.success(ObjectUtils.getJSONArray(System_Permission.config,list));
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    /**
     * 修改权限
     * @throws IOException
     */
    @RequestMapping("/addOrUpdatePermission")
    public ResultVO addOrUpdatePermission(System_Permission model,HttpServletRequest request,HttpServletResponse response) throws IOException{
        JSONObject obj = new JSONObject();
        try{
            if(StringUtils.isEmpty(model.getId())){
                permissionServiceImpl.addPermission(model);
            }else {
                permissionServiceImpl.updatePermission(model);
            }
            return ResultVO.success();
        }catch (Exception e) {
            obj.put("status", 0);
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

    @RequestMapping("/updatePermissionStatus")
    public ResultVO updatePermissionStatus(System_Permission model,HttpServletRequest request,HttpServletResponse response) throws IOException{
        JSONObject obj = new JSONObject();
        try{
            permissionServiceImpl.updatePermissionStatus(model);
            return ResultVO.success();
        }catch (Exception e) {
            obj.put("status", 0);
            e.printStackTrace();
        }
        return ResultVO.failed();
    }

}
